{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Find Words That Can Be Formed by Characters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: countCharacters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #拼写单词"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一份『词汇表』（字符串数组）&nbsp;<code>words</code>&nbsp;和一张『字母表』（字符串）&nbsp;<code>chars</code>。</p>\n",
    "\n",
    "<p>假如你可以用&nbsp;<code>chars</code>&nbsp;中的『字母』（字符）拼写出 <code>words</code>&nbsp;中的某个『单词』（字符串），那么我们就认为你掌握了这个单词。</p>\n",
    "\n",
    "<p>注意：每次拼写（指拼写词汇表中的一个单词）时，<code>chars</code> 中的每个字母都只能用一次。</p>\n",
    "\n",
    "<p>返回词汇表&nbsp;<code>words</code>&nbsp;中你掌握的所有单词的 <strong>长度之和</strong>。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>words = [&quot;cat&quot;,&quot;bt&quot;,&quot;hat&quot;,&quot;tree&quot;], chars = &quot;atach&quot;\n",
    "<strong>输出：</strong>6\n",
    "<strong>解释： </strong>\n",
    "可以形成字符串 &quot;cat&quot; 和 &quot;hat&quot;，所以答案是 3 + 3 = 6。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>words = [&quot;hello&quot;,&quot;world&quot;,&quot;leetcode&quot;], chars = &quot;welldonehoneyr&quot;\n",
    "<strong>输出：</strong>10\n",
    "<strong>解释：</strong>\n",
    "可以形成字符串 &quot;hello&quot; 和 &quot;world&quot;，所以答案是 5 + 5 = 10。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ol>\n",
    "\t<li><code>1 &lt;= words.length &lt;= 1000</code></li>\n",
    "\t<li><code>1 &lt;= words[i].length, chars.length&nbsp;&lt;= 100</code></li>\n",
    "\t<li>所有字符串中都仅包含小写英文字母</li>\n",
    "</ol>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [find-words-that-can-be-formed-by-characters](https://leetcode.cn/problems/find-words-that-can-be-formed-by-characters/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [find-words-that-can-be-formed-by-characters](https://leetcode.cn/problems/find-words-that-can-be-formed-by-characters/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"cat\",\"bt\",\"hat\",\"tree\"]\\n\"atach\"', '[\"hello\",\"world\",\"leetcode\"]\\n\"welldonehoneyr\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        \n",
    "        ans = 0\n",
    "        for i in words:\n",
    "            dict2 = Counter(chars)\n",
    "            a = 0\n",
    "            for x in i:\n",
    "                if x in dict2:\n",
    "                    dict2[x] -= 1\n",
    "                    if dict2[x] < 0:\n",
    "                        a = -1\n",
    "                        break\n",
    "                else:\n",
    "                    a = -1\n",
    "                    break\n",
    "            if a == 0:\n",
    "                ans += len(i)\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        def bianhaxi(zifu):\n",
    "            zidian={}\n",
    "            for i in zifu:\n",
    "                if i in zidian.keys():\n",
    "                    zidian[i]+=1\n",
    "                else:\n",
    "                    zidian[i]=1\n",
    "            return zidian\n",
    "        aa=list(map(bianhaxi,words))\n",
    "        biaozhun=bianhaxi(chars)\n",
    "        ans=0\n",
    "        for i in aa:\n",
    "            n=0\n",
    "            for k,v in i.items():\n",
    "                if k in biaozhun.keys() and v<=biaozhun[k]:\n",
    "                    n+=1\n",
    "                else:\n",
    "                    break\n",
    "            if n==len(i.keys()):\n",
    "                ans+=sum(i.values())\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def countCharacters(self, words, chars):\n",
    "        \"\"\"\n",
    "        :type words: List[str]\n",
    "        :type chars: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        w = [collections.Counter(word) for word in words]\n",
    "        c = collections.Counter(chars)\n",
    "        t = 0\n",
    "        for i in range(len(words)):\n",
    "            if not w[i] - c:\n",
    "                t += len(words[i])\n",
    "        return t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        chars_count = Counter(chars)\n",
    "        total_length = 0\n",
    "\n",
    "        for word in words:\n",
    "            word_count = Counter(word)\n",
    "            possible_to_spell = True\n",
    "            for char,count in word_count.items():\n",
    "                if chars_count[char] < count:\n",
    "                    possible_to_spell = False\n",
    "                    break\n",
    "            if possible_to_spell:\n",
    "                total_length += len(word) \n",
    "        return total_length       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        Hash_words, Hash_chars = {}, {}\n",
    "        for i in range(len(words)):\n",
    "            word = {}\n",
    "            for j in words[i]:\n",
    "                word[j] = word.get(j, 0) + 1\n",
    "            Hash_words[i] = word\n",
    "        for k in chars:\n",
    "            Hash_chars[k] = Hash_chars.get(k, 0) + 1\n",
    "        res = 0\n",
    "        for key in Hash_words.keys():\n",
    "            w =  Hash_words[key]\n",
    "            s = 0\n",
    "            for l in w.keys():\n",
    "                if Hash_chars.get(l) is None:\n",
    "                    break\n",
    "                else:\n",
    "                    if Hash_chars[l] < w[l]:\n",
    "                        break\n",
    "                    else:\n",
    "                        s += 1\n",
    "            if s == len(w):\n",
    "                res += len(words[key])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# # 480ms\n",
    "# class Solution:\n",
    "#     def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "#         hashtable = {}\n",
    "#         for c in chars:\n",
    "#             hashtable[c] = hashtable.get(c, 0) + 1\n",
    "#         res = 0\n",
    "#         for word in words:\n",
    "#             ht = deepcopy(hashtable)\n",
    "#             i = 0\n",
    "#             for w in word:\n",
    "#                 if ht.get(w, 0) >0:\n",
    "#                     if ht[w] > 0:\n",
    "#                         ht[w] -= 1\n",
    "#                         i += 1\n",
    "#                 else: \n",
    "#                     break\n",
    "#                 if i == len(word):\n",
    "#                     res += len(word)\n",
    "#         return res\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words, chars):\n",
    "        char_count = {}\n",
    "        for c in chars:\n",
    "            char_count[c] = char_count.get(c, 0) + 1\n",
    "        \n",
    "        total_length = 0\n",
    "        \n",
    "        for word in words:\n",
    "            word_count = {}\n",
    "            for w in word:\n",
    "                word_count[w] = word_count.get(w, 0) + 1\n",
    "            \n",
    "            # Check if the word can be formed from available characters\n",
    "            can_form = True\n",
    "            for char, count in word_count.items():\n",
    "                if char_count.get(char, 0) < count:\n",
    "                    can_form = False\n",
    "                    break\n",
    "            \n",
    "            if can_form:\n",
    "                total_length += len(word)\n",
    "                \n",
    "        return total_length\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        c = Counter(chars)\n",
    "        return sum(map(len, filter(lambda w: Counter(w) <= c, words)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        result=0\n",
    "        jia=Counter(chars)  \n",
    "        for w in words:\n",
    "            c=Counter(w)                     \n",
    "            if all(c[i]<=jia[i] for i in c): \n",
    "                result+=len(w)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "       return sum(len(w) \n",
    "                    for w in words \n",
    "                    if Counter(chars)&Counter(w) == Counter(w))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        res = 0\n",
    "        for word in words:\n",
    "            for i in word:\n",
    "                if word.count(i) > chars.count(i):\n",
    "                    break\n",
    "            else:\n",
    "                res += len(word)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 480ms\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        hashtable = {}\n",
    "        for c in chars:\n",
    "            hashtable[c] = hashtable.get(c, 0) + 1\n",
    "        res = 0\n",
    "        for word in words:\n",
    "            print(word)\n",
    "            ht = deepcopy(hashtable)\n",
    "            i = 0\n",
    "            word_count = {}\n",
    "            for w in word:\n",
    "                word_count[w] = word_count.get(w, 0) + 1\n",
    "            can_form = True\n",
    "            for w, count in word_count.items():\n",
    "                if ht.get(w, 0) < count:\n",
    "                    can_form = False\n",
    "                    break\n",
    "            if can_form == True:\n",
    "                res += len(word)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        char = Counter(chars)\n",
    "        ans = 0\n",
    "        for word in words:\n",
    "            cnt = Counter(word)\n",
    "            for c in cnt:\n",
    "                if cnt[c] > char[c]:\n",
    "                    break\n",
    "            else:\n",
    "                ans += len(word)\n",
    "        return ans\n",
    "        \n",
    "        # char = Counter(chars)\n",
    "        # ans = 0\n",
    "        # for word in words:\n",
    "        #     cnt = Counter(word)\n",
    "        #     check = True\n",
    "        #     for key in cnt.keys():\n",
    "        #         if cnt[key] > char[key]:\n",
    "        #             check = False\n",
    "        #             break\n",
    "        #     if check:\n",
    "        #         ans += len(word)\n",
    "        # return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        cnt=0\n",
    "        dict1=Counter(chars)\n",
    "        for i in words:\n",
    "            dict2=Counter(i)\n",
    "            for j in i:\n",
    "                if dict2[j]>dict1[j]:\n",
    "                    break\n",
    "            else:\n",
    "                cnt+=len(i)\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        mydic = Counter(chars)\n",
    "        sum = 0\n",
    "        for word in words:\n",
    "            dic = Counter(word)\n",
    "            for i in dic:\n",
    "                if i not in mydic or dic[i] > mydic[i]:\n",
    "                    break\n",
    "            else:\n",
    "                sum=sum+len(word)\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        char_dict = {}\n",
    "        for i in chars:\n",
    "            if i in char_dict:\n",
    "                char_dict[i] += 1\n",
    "            else:\n",
    "                char_dict[i] = 1\n",
    "    \n",
    "        sum_length = 0\n",
    "    \n",
    "        for i in words:\n",
    "            word_dict = {}\n",
    "            for j in i:\n",
    "                if j in word_dict:\n",
    "                    word_dict[j] += 1\n",
    "                else:\n",
    "                    word_dict[j] = 1\n",
    "            for m in word_dict:\n",
    "                if m in char_dict:\n",
    "                    if char_dict[m] >= word_dict[m]:\n",
    "                        continue\n",
    "                    else:\n",
    "                        break\n",
    "                else:\n",
    "                    break\n",
    "            else:\n",
    "                sum_length += len(i)\n",
    "\n",
    "        return sum_length"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "       return sum(len(w) \n",
    "                    for w in words \n",
    "                    if Counter(chars)&Counter(w) == Counter(w))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        \"\"\"\n",
    "        要求所有掌握的单词的长度。chartes中每个字母都只能用一次，\n",
    "        \"\"\"\n",
    "        # 遍历单词数组\n",
    "        # ans = 0\n",
    "\n",
    "        # for word in words:\n",
    "        #     charscp = list(chars) # 重置charscp 并且列表化\n",
    "        #     count = 0\n",
    "        #     for i in word:\n",
    "        #         if i in charscp:\n",
    "        #             count += 1 # 当count = len(word)全都在\n",
    "        #             charscp.remove(i)\n",
    "        #     if count == len(word):\n",
    "        #         ans += count    \n",
    "        # return ans\n",
    "        \"\"\"\n",
    "        哈希表计数\n",
    "        对于一个单词word 只要word中每个字母的个数，不大于chars中的对应字母个数，那么就可用用chars中的字母拼写出来\n",
    "        我们用一个哈希表存储chars中每个字母的数量，再用一个哈希表存储word中的每个字母数量，最后两个哈希表的键值对\n",
    "        逐一比较即可\n",
    "        \"\"\"\n",
    "        chars_cnt = collections.Counter(chars)\n",
    "        ans = 0\n",
    "        for word in words:\n",
    "            word_cnt = collections.Counter(word)\n",
    "            for c in word_cnt:\n",
    "                if chars_cnt[c] < word_cnt[c]:\n",
    "                    break # 不能拼写出来\n",
    "            else:\n",
    "                ans += len(word)\n",
    "        return ans        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "       return sum(len(w) \n",
    "                    for w in words \n",
    "                    if Counter(chars)&Counter(w) == Counter(w))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\r\n",
    "        from collections import Counter\r\n",
    "        ans = 0\r\n",
    "        chars = Counter(chars)\r\n",
    "        for i in words:\r\n",
    "            j = Counter(i)\r\n",
    "            if all(chars[k] >= j[k] for k in j):\r\n",
    "                ans += len(i)\r\n",
    "        return ans\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        a = 0\n",
    "        c = collections.Counter(chars)\n",
    "        for w in words:\n",
    "            c2 = collections.Counter(w)\n",
    "            if all([c2[i] <= c[i] for i in c2]):\n",
    "                a += len(w)\n",
    "        return a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        helper = collections.Counter(chars)\n",
    "        ans = 0\n",
    "        for word in words:\n",
    "            w = collections.Counter(word)\n",
    "            for i in w:\n",
    "                if w[i] > helper[i]:\n",
    "                    break\n",
    "            else:\n",
    "                ans += len(word)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        i = 0\n",
    "        cnt = collections.Counter(chars)\n",
    "        for w in words:\n",
    "            c = collections.Counter(w)\n",
    "            if all([c[i] <= cnt[i] for i in c]):\n",
    "                i += len(w)\n",
    "        return i\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        char = Counter(chars)\n",
    "        ans = 0\n",
    "        for word in words:\n",
    "            cnt = Counter(word)\n",
    "            check = True\n",
    "            for key in cnt.keys():\n",
    "                if cnt[key] > char[key]:\n",
    "                    check = False\n",
    "                    break\n",
    "            if check:\n",
    "                ans += len(word)\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        c = Counter(chars)\n",
    "        return sum(len(w) for w in words if Counter(w) <= c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        cnt_chars = collections.Counter(chars)\n",
    "        res = 0\n",
    "        for word in words:\n",
    "            cnt_word = collections.Counter(word)\n",
    "            if all(cnt_word[i] <= cnt_chars[i] for i in word):\n",
    "                res += len(word)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        lc=0\n",
    "        cl=list(chars)\n",
    "        t=[]\n",
    "        r=[]\n",
    "        for i in words:\n",
    "            flag=True\n",
    "            for j in i:\n",
    "                if j in cl:                    \n",
    "                    t.append(cl.pop(cl.index(j)))\n",
    "                else:\n",
    "                    flag=False\n",
    "                    break\n",
    "            cl.extend(t)\n",
    "            t.clear()\n",
    "            if flag:\n",
    "                r.append(i)\n",
    "        for c in r:\n",
    "            lc+=len(c)\n",
    "        return lc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "    \n",
    "        ans = 0\n",
    "        cnt = collections.Counter(chars)\n",
    "        for w in words:\n",
    "            c = collections.Counter(w)\n",
    "            if all([c[i] <= cnt[i] for i in c]):\n",
    "                ans += len(w)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        chars_cnt = collections.Counter(chars)\n",
    "        ans = 0\n",
    "        for word in words:\n",
    "            word_cnt = collections.Counter(word)\n",
    "            for c in word_cnt:\n",
    "                if chars_cnt[c] < word_cnt[c]:\n",
    "                    break\n",
    "            else:\n",
    "                ans += len(word)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        c = Counter(chars)\n",
    "        l = 0\n",
    "\n",
    "        for word in words:\n",
    "            t = Counter(word)\n",
    "            f = 1\n",
    "            for i in t:\n",
    "                if t[i] > c[i]:\n",
    "                    f = 0\n",
    "                    break\n",
    "            if f:\n",
    "                l += len(word)\n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        l=0\n",
    "        cc=Counter(chars)\n",
    "        for i in words:\n",
    "            if Counter(i)<cc:\n",
    "                l+=len(i)\n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        res=0\n",
    "        c=Counter(chars)\n",
    "        for i in words:\n",
    "            if c>=Counter(i):\n",
    "                res+=len(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        c = Counter(chars)\n",
    "        l = 0\n",
    "\n",
    "        for word in words:\n",
    "            t = Counter(word)\n",
    "            f = 1\n",
    "            for i in t:\n",
    "                if t[i] > c[i]:\n",
    "                    f = 0\n",
    "                    break\n",
    "            if f:\n",
    "                l += len(word)\n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        h = collections.Counter(chars)\n",
    "        h0 = h.copy()\n",
    "        res = 0\n",
    "        for word in words:\n",
    "            ans = True\n",
    "            for s in word:\n",
    "                if s in h and h[s] > 0:\n",
    "                    h[s] -= 1\n",
    "                else:\n",
    "                    ans = False\n",
    "                    break\n",
    "            if ans:\n",
    "                res += len(word)\n",
    "            h = h0.copy()\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        li = []\n",
    "        for i in words:\n",
    "            chars1 = chars\n",
    "            sign = False\n",
    "            for j in i:\n",
    "                if j in chars1:\n",
    "                     chars1 = chars1.replace(j, '', 1)\n",
    "                else:\n",
    "                    sign = False\n",
    "                    break\n",
    "                sign = True\n",
    "            if sign is True:\n",
    "                li.append(i)\n",
    "        length = 0\n",
    "        for items in li:\n",
    "            length += len(items)\n",
    "        return length\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        chars_cnt = collections.Counter(chars)\n",
    "        ans = 0\n",
    "        flag = 1\n",
    "        for word in words:\n",
    "            word_cnt = collections.Counter(word)\n",
    "            for c in word_cnt:\n",
    "                if chars_cnt[c] < word_cnt[c]:\n",
    "                    flag = 0\n",
    "                    break\n",
    "            else:\n",
    "                ans += len(word)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        count=0\n",
    "        for i in words:\n",
    "            t=0\n",
    "            for s in i:\n",
    "                if i.count(s)<=chars.count(s):\n",
    "                    t+=1\n",
    "            if t==len(i):\n",
    "                count+=t\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        # 思路： 遍历words word，如果Counter(chars)>Counter(word)，返回word的长度，累加给ans\n",
    "        ans = 0\n",
    "        char = Counter(chars)\n",
    "        for word in words:\n",
    "            if Counter(word) < char:\n",
    "                ans += len(word)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        c = collections.Counter(chars)\n",
    "        sun = 0\n",
    "        for word in words:\n",
    "            w = collections.Counter(word)\n",
    "            if c&w == Counter(w):\n",
    "                sun += len(word)\n",
    "        return sun"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        cnt_chars = collections.Counter(chars)\n",
    "        res = 0\n",
    "        for word in words:\n",
    "            cnt_word = collections.Counter(word)\n",
    "            if all(cnt_word[i] <= cnt_chars[i] for i in word):\n",
    "                res += len(word)\n",
    "        return res\n",
    "\n",
    "#https://leetcode.cn/problems/find-words-that-can-be-formed-by-characters/solutions/21708/tong-ji-python3-by-smoon1989/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        chars_counter=collections.Counter(chars)\n",
    "        \n",
    "        ans=0\n",
    "        for w in words:\n",
    "            w_counter=collections.Counter(w)\n",
    "            if all(w_counter[c] <= chars_counter[c] for c in w_counter):\n",
    "                ans+=len(w)\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        total = 0\n",
    "        for item in words:\n",
    "            for ch in set(item):\n",
    "                if item.count(ch) > chars.count(ch):\n",
    "                    break\n",
    "            else:\n",
    "                total += len(item)\n",
    "        return total\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        chars_cnt = collections.Counter(chars)\n",
    "        ans = 0\n",
    "        for word in words:\n",
    "            word_cnt = collections.Counter(word)\n",
    "            for c in word_cnt:\n",
    "                if chars_cnt[c] < word_cnt[c]:\n",
    "                    break\n",
    "            else:\n",
    "                ans += len(word)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "      pattern = collections.defaultdict(int)\n",
    "      for ch in chars:  pattern[ch] += 1\n",
    "\n",
    "      cnt = 0\n",
    "      for word in words:\n",
    "        if len(word) > len(chars):\n",
    "          continue\n",
    "        \n",
    "        cur = collections.defaultdict(int)\n",
    "        flag = True\n",
    "        for ch in word:\n",
    "          cur[ch] += 1\n",
    "          if cur[ch] > pattern[ch]:\n",
    "            flag = False\n",
    "            break\n",
    "        \n",
    "        if flag:  cnt += len(word)\n",
    "      return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        c = Counter(chars)\n",
    "        return sum(len(word) for word in words if Counter(word) <= c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        count=0\n",
    "        for i in words:\n",
    "            t=0\n",
    "            for s in i:\n",
    "                if i.count(s)<=chars.count(s):\n",
    "                    t+=1\n",
    "            if t==len(i):\n",
    "                count+=t\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "            chars_cnt = collections.Counter(chars)\n",
    "            #print(chars_cnt)\n",
    "            ans = 0\n",
    "            for word in words:\n",
    "                word_cnt = collections.Counter(word)\n",
    "                for c in word_cnt:\n",
    "                    if chars_cnt[c] < word_cnt[c]:\n",
    "                        break\n",
    "                else:\n",
    "                    ans += len(word)\n",
    "            return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        c = Counter(chars)\n",
    "\n",
    "        res = 0\n",
    "        for word in words:\n",
    "            w = Counter(word)\n",
    "            if len(c - w) >= 0 and len(w - c) == 0:\n",
    "                res += len(word)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        m={}\n",
    "        for each in chars:\n",
    "            if each not in m:\n",
    "                m[each]=1\n",
    "            else:\n",
    "                m[each]+=1\n",
    "        count=0\n",
    "        for each in words:\n",
    "            flag=1\n",
    "            x=m.copy()\n",
    "            for i in each:\n",
    "                if i not in x:\n",
    "                    flag=0\n",
    "                    break\n",
    "                x[i]-=1\n",
    "                if x[i]<0:\n",
    "                    flag=0\n",
    "                    break\n",
    "            if flag==1:\n",
    "                count+=len(each)\n",
    "        return count\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        res = 0\n",
    "        char_dict = collections.Counter(chars)\n",
    "        for word in words:\n",
    "            word_dict = collections.Counter(word)\n",
    "            for c in word_dict:\n",
    "                if char_dict[c] < word_dict[c]:\n",
    "                    break\n",
    "            else:\n",
    "                res += len(word)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        result = 0\n",
    "        dic = {}\n",
    "        for i in chars:\n",
    "            dic[i] = chars.count(i)\n",
    "\n",
    "        for i in words:\n",
    "            for j in range(len(i)):\n",
    "                if i[j] in dic.keys() and i.count(i[j]) <= dic.get(i[j]):\n",
    "                    if j == len(i) - 1:\n",
    "                        result += len(i)\n",
    "                else:\n",
    "                    break\n",
    "        return result\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        ans = 0\n",
    "        cnt = collections.Counter(chars)\n",
    "        for word in words:\n",
    "            c = collections.Counter(word)\n",
    "            if all([c[i] <= cnt[i] for i in c]):\n",
    "                ans += len(word)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        ori_list_chars = []\n",
    "        for letter in chars:\n",
    "            ori_list_chars.append(letter)\n",
    "        ori_list_chars = tuple(ori_list_chars)\n",
    "        spelled = []\n",
    "        num = 0\n",
    "        list_chars = []\n",
    "        for word in words:\n",
    "            list_chars = list(ori_list_chars)\n",
    "            for letter in word:\n",
    "                if letter in list_chars:\n",
    "                    list_chars.pop(list_chars.index(str(letter)))\n",
    "            if len(ori_list_chars)-len(list_chars) == len(word):\n",
    "                spelled.append(word)\n",
    "        for word in spelled:\n",
    "            num += len(word)\n",
    "        return num\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        dic = Counter(chars)\n",
    "        ans = 0\n",
    "        for word in words:\n",
    "            c = Counter(word)\n",
    "            if all(c[i] <= dic[i] for i in word):\n",
    "                ans += len(word)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        total = 0\n",
    "        def count_ch(chs: str):\n",
    "            item_count = {}\n",
    "            for ch in chs:\n",
    "                if ch in item_count:\n",
    "                    item_count[ch] += 1\n",
    "                else:\n",
    "                    item_count[ch] = 1\n",
    "            return item_count\n",
    "        chars_count = count_ch(chars)\n",
    "        for item in words:\n",
    "            quantities = count_ch(item)\n",
    "            for key, val in quantities.items():\n",
    "                if not chars_count.get(key) or chars_count.get(key) < val:\n",
    "                    break\n",
    "            else:\n",
    "                total += len(item)\n",
    "        return total\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        c_counter=Counter(chars)\n",
    "        ans=0\n",
    "        for w in words:\n",
    "            w_counter=Counter(w)\n",
    "           \n",
    "            if all(c_counter[i]>=w_counter[i]  for i in w_counter):\n",
    "                ans+=len(w)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        c = Counter(chars)\n",
    "        l = 0\n",
    "\n",
    "        for word in words:\n",
    "            t = Counter(word)\n",
    "            f = 1\n",
    "            for i in t:\n",
    "                if t[i] > c[i]:\n",
    "                    f = 0\n",
    "                    break\n",
    "            if f:\n",
    "                l += len(word)\n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        ans = 0\n",
    "        count = {}\n",
    "        for char in chars:\n",
    "            count[char] = count.get(char, 0) + 1\n",
    "\n",
    "        for word in words:\n",
    "            word_count = {}\n",
    "            for char in word:\n",
    "                word_count[char] = word_count.get(char, 0) + 1\n",
    "            flag = True\n",
    "            for count_char in word_count:\n",
    "                \n",
    "                if count_char not in count or word_count[count_char] > count[count_char]:\n",
    "                    flag = False\n",
    "                    break\n",
    "            if flag:\n",
    "                ans += len(word)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        from collections import Counter\n",
    "        total = 0\n",
    "        chs = Counter(chars)\n",
    "        for item in words:\n",
    "            quantities = Counter(item)\n",
    "            for key,val in quantities.items():\n",
    "                chs_val = chs.get(key)\n",
    "                if not chs_val:\n",
    "                    break\n",
    "                if chs_val < val:\n",
    "                    break\n",
    "            else:\n",
    "                total += quantities.total()\n",
    "        return total\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        count = collections.Counter(chars)\n",
    "        print(count)\n",
    "        ans = 0\n",
    "        for word in words:\n",
    "            word_count = collections.Counter(word)\n",
    "            flag = True\n",
    "            for c in word:\n",
    "                if count[c] < word_count[c]:\n",
    "                    flag = False\n",
    "                    break\n",
    "            if flag:\n",
    "                ans += len(word)\n",
    "        return ans\n",
    "        \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        r=0\n",
    "        char=dict(Counter(chars))\n",
    "        for word in words:\n",
    "            chars =char.copy()\n",
    "            for i in word:\n",
    "                if i not in chars:\n",
    "                    break\n",
    "                if chars[i]==0:\n",
    "                    break\n",
    "                chars[i]-=1\n",
    "            else:\n",
    "                r += len(word)\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        c = Counter(chars)\n",
    "        l = 0\n",
    "\n",
    "        for word in words:\n",
    "            t = Counter(word)\n",
    "            f = 1\n",
    "            for i in t:\n",
    "                if t[i] > c[i]:\n",
    "                    f = 0\n",
    "                    break\n",
    "            if f:\n",
    "                l += len(word)\n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        c = Counter(chars)\n",
    "        l = 0\n",
    "\n",
    "        for word in words:\n",
    "            t = Counter(word)\n",
    "            f = 1\n",
    "            for i in t:\n",
    "                if t[i] > c[i]:\n",
    "                    f = 0\n",
    "                    break\n",
    "            if f:\n",
    "                l += len(word)\n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        cnt = Counter(chars)\n",
    "        def check_word(w):\n",
    "            cnt_w = Counter(w)\n",
    "            for c in set(cnt_w):\n",
    "                if cnt[c] is None or cnt[c] < cnt_w[c]:\n",
    "                    return False\n",
    "            return True\n",
    "\n",
    "        ans = 0\n",
    "        for w in words:\n",
    "            if check_word(w): ans += len(w)\n",
    "        return ans\n",
    "            \n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        chars_dict = {}\n",
    "        result = 0\n",
    "        for ch in chars:\n",
    "            if ch in chars_dict:\n",
    "                chars_dict[ch] += 1\n",
    "            else:\n",
    "                chars_dict[ch] = 1\n",
    "        for word in words:\n",
    "            word_dict = dict(chars_dict)\n",
    "            result += len(word)\n",
    "            for letter in word:\n",
    "                if (letter not in word_dict) or (word_dict[letter] <= 0):\n",
    "                    result -= len(word)\n",
    "                    break\n",
    "                else:\n",
    "                    word_dict[letter] -= 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        res = 0\n",
    "        \n",
    "        for word in words:\n",
    "            if len(set(word))>len(set(chars)):\n",
    "                continue\n",
    "            length = 0\n",
    "            tmp = list(chars)\n",
    "            \n",
    "            for char in word:\n",
    "                if char in tmp:\n",
    "                    length +=1\n",
    "                    tmp.remove(char)\n",
    "                    \n",
    "                \n",
    "                \n",
    "            if length ==len(word):\n",
    "                res+=length\n",
    "            \n",
    "        return res\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        char_dic = collections.defaultdict(int)\n",
    "        for i in chars:\n",
    "            char_dic[i] += 1\n",
    "        res = 0\n",
    "        for i in words:\n",
    "            tmp_dic = collections.defaultdict(int)\n",
    "            for s in i:\n",
    "                tmp_dic[s] += 1\n",
    "            can_flag = 1\n",
    "            for tmp_s in tmp_dic.keys():\n",
    "                if tmp_dic[tmp_s] <= char_dic[tmp_s]:\n",
    "                    continue\n",
    "                else:\n",
    "                    can_flag =0\n",
    "                    break\n",
    "            if can_flag:\n",
    "                res += len(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        chars_cnt = collections.Counter(chars)\n",
    "        ans = 0\n",
    "        for word in words:\n",
    "            word_cnt = collections.Counter(word)\n",
    "            for c in word_cnt:\n",
    "                if chars_cnt[c] < word_cnt[c]:\n",
    "                    break\n",
    "            else:\n",
    "                ans += len(word)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCharacters(self, words: List[str], chars: str) -> int:\n",
    "        from collections import Counter\n",
    "        total = 0\n",
    "        chs = Counter(chars)\n",
    "        for item in words:\n",
    "            ch = chs.copy()\n",
    "            quantities = Counter(item)\n",
    "            ch.subtract(quantities)\n",
    "            for _, val in ch.items():\n",
    "                if val < 0:\n",
    "                    break\n",
    "            else:\n",
    "                total += quantities.total()\n",
    "        return total\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
